package com.cloudwise.aop;

import com.cloudwise.entity.Log;
import com.cloudwise.entity.User;
import com.cloudwise.service.LogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;

/**
 * @author:高杰领
 * @date:2022/1/20 10:24
 * @description:日志切面类
 * @version:1.0
 **/
@EnableAspectJAutoProxy
@Component
@Aspect
public class LogAspect {
    @Resource
    private LogService logService;

    @Pointcut("execution(* com.cloudwise.controller.*.*(..))")
    public void a() {
    }

    @Around("a()")
    public Object log(ProceedingJoinPoint joinPoint) {
        //获取方法参数
        Object[] args = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        //保存日志信息
        Log log = new Log();
        Subject subject = SecurityUtils.getSubject();
        Object principal = subject.getPrincipal();
        User user = new User();
        if (principal != null) {
            user = (User) principal;
        }
        log.setLoginName(user.getLoginName());
        log.setUserName(user.getUserName());
        //获取请求的ip地址
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String ipAddress = getIpAddress(request);
        log.setIp(ipAddress);
        String className = joinPoint.getTarget().getClass().getName();
        log.setClassName(className);
        String[] split = signature.toString().split("\\.");
        String method = split[split.length - 1];
        log.setMethodName(method);
        //获取参数
        String params = Arrays.toString(args);
        if (params.getBytes().length > 8889) {
            log.setParams("Too Long Params To Save ");
        } else {
            log.setParams(params);
        }
        //设置时间
        log.setOperationTime(new Date());
        logService.save(log);
        try {
            //业务方法执行
            Object proceed = joinPoint.proceed(args);
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }


    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
